//
//  WorksTableViewCell.m
//  SoundPill
//
//  Created by 宋旭东 on 15/6/23.
//  Copyright (c) 2015年 宋旭东. All rights reserved.
//

#import "WorksTableViewCell.h"
#import "SDWebImageManager.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "AppDelegate.h"
#define kleftGap 25 //x
#define kupGap   30 //y
#define kImageViewWidth [UIScreen mainScreen].bounds.size.width*0.28

#define kImageViewHeight [UIScreen mainScreen].bounds.size.width*0.28
#define kLabelWidth [UIScreen mainScreen].bounds.size.width*0.5
#define kLabelHeight 55

@interface WorksTableViewCell ()
@property (strong, nonatomic) UIView * edging;
@property (strong, nonatomic) UIView *blackBk;
@property (strong, nonatomic) UIImageView *timeMin;
@property (strong, nonatomic) UIImageView *timeSec;
@property (strong, nonatomic) UILabel *classify;
@end

@implementation WorksTableViewCell
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self initSubViews];
        self.backgroundColor=[UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}
- (void)initSubViews
{
    
    // 图片
    _imgView = [[UIImageView alloc] initWithFrame:CGRectZero];
 
    _imgView.image = [UIImage imageNamed:@"list_defaultImage"];
    _imgView.backgroundColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.07];
    [self.contentView addSubview:_imgView];
    self.blackBk = [[UIView alloc] initWithFrame:CGRectZero];
    _blackBk.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.330];
    [_imgView addSubview:_blackBk];
    
    titleView = [[UIView alloc]initWithFrame:CGRectZero];
    titleView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:titleView];
    // 标题
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    //_titleLabel.backgroundColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.07];
//    _titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
    _titleLabel.backgroundColor = [UIColor clearColor];
    _titleLabel.numberOfLines=0;
    //_titleLabel.contentMode = UIViewContentModeTop;
    _titleLabel.font = [UIFont boldSystemFontOfSize:18];
    [titleView addSubview:_titleLabel];

    // 播放次数
    _playtimesBtn = [[spItem alloc] initWithFrame:CGRectZero];
    //_playtimesBtn.font = [UIFont systemFontOfSize:14];
    //_playtimesBtn.backgroundColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.07];
    
    [self.contentView addSubview:_playtimesBtn];
    
    
    //点按数量
    _heartCountBtn = [[heartItem alloc]initWithFrame:CGRectZero];
    
//    [_heartCountBtn addTarget:self action:@selector(loveIt) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self.contentView addSubview:_heartCountBtn];
    
    
    //时间轴线
    _timeLine = [[UIImageView alloc]initWithFrame:CGRectMake(16, 0, 1.5, 200)];
    _timeLine.backgroundColor = [UIColor blackColor];
    [self.contentView addSubview:_timeLine];
    
    
    //时间轴点
    _timePoint = [[UIImageView alloc]initWithFrame:CGRectMake(9.2, 8, 15, 15)];
    _timePoint.image = [UIImage imageNamed:@"gray_point"];
    [self.contentView addSubview:_timePoint];
    
    
    //    时间Lable
    //     月
    _monthLabel = [[UILabel alloc] initWithFrame:CGRectMake(kleftGap + 5, 0, 50, 30)];
    _monthLabel.textAlignment = NSTextAlignmentLeft;
    _monthLabel.backgroundColor = [UIColor clearColor];
    _monthLabel.textColor = [UIColor grayColor];
    _monthLabel.font = [UIFont boldSystemFontOfSize:12];
    
    [self.contentView addSubview:_monthLabel];
    
    self.classify = [[UILabel alloc] initWithFrame:CGRectZero];
    _classify.textAlignment = NSTextAlignmentRight;
    _classify.textColor = [UIColor grayColor];
    _classify.font = [UIFont boldSystemFontOfSize:13];
    [self.contentView addSubview:_classify];
    
    
    // 作品时长
    _timeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    _timeLabel.font = [UIFont boldSystemFontOfSize:24];
    _timeLabel.textColor = [UIColor whiteColor];
    [_imgView addSubview:_timeLabel];
    self.timeMin = [[UIImageView alloc] initWithFrame:CGRectZero];
    _timeMin.image = [UIImage imageNamed:@"myself_min"];
    [_imgView addSubview:_timeMin];
    self.timeSec = [[UIImageView alloc] initWithFrame:CGRectZero];
    _timeSec.image = [UIImage imageNamed:@"myself_sec"];
    [_imgView addSubview:_timeSec];
    
    self.edging = [[UIView alloc] initWithFrame:CGRectZero];
    [self addSubview:_edging];
    
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    _timeLine.frame = CGRectMake(16, 0, 1.5, self.frame.size.height);
    //首图预览
    _imgView.frame = CGRectMake(kleftGap, kupGap, kImageViewWidth, kImageViewHeight);
    //_imgView.backgroundColor=[UIColor whiteColor];
    _imgView.image=[UIImage imageNamed:@"list_defaultImage"];
    if (self.listdataModel.cover!=nil) {
        [_imgView setImageWithURL:[NSURL URLWithString:_listdataModel.cover] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
    }
    [_imgView setContentScaleFactor:[[UIScreen mainScreen] scale]];
    _imgView.contentMode =  UIViewContentModeScaleAspectFill;
    _imgView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    _imgView.clipsToBounds  = YES;
    _blackBk.frame = _imgView.bounds;
    
    
    _whiteView = [[UIView alloc]initWithFrame:CGRectMake(_imgView.right, kupGap, 5,kLabelHeight-1)];
    //    _whiteView.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.070];
    _whiteView.backgroundColor = [UIColor whiteColor];
    
    [self.contentView addSubview:_whiteView];
    
    
    titleView.frame = CGRectMake(_whiteView.right + 5, _imgView.top, kLabelWidth-7, kLabelHeight);
    // 标题
    _titleLabel.text  = self.listdataModel.title;
//    _titleLabel.text=@"你离开了南京，从此没有人跟我说爱你啊啊啊啊啊啊啊啊";
    CGSize size = CGSizeMake(kLabelWidth - 7, 50);
//    CGSize labelsize = [_listdataModel.title sizeWithFont:[UIFont boldSystemFontOfSize:18] constrainedToSize:size lineBreakMode:NSLineBreakByWordWrapping];
    NSDictionary *attribute = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:18]};
    CGSize labelsize = [_titleLabel.text boundingRectWithSize:size options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
    _titleLabel.frame = CGRectMake(0, 10, labelsize.width, labelsize.height);
    
    
    
    
    UIView *rightWhiteGap = [[UIView alloc]initWithFrame:CGRectMake(titleView.right-1, _imgView.top, 7.6, kLabelHeight-1)];
    //rightWhiteGap.backgroundColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.07];
    rightWhiteGap.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:rightWhiteGap];
    
    
    //分割线
    UIImageView *upLine = [[UIImageView alloc]initWithFrame:CGRectMake(_imgView.right, _imgView.top, kLabelWidth+_whiteView.width, 1)];
    upLine.image = [UIImage imageNamed:@"gray_line"];
    //[self.contentView addSubview:upLine];
    UIImageView *rightLine = [[UIImageView alloc]initWithFrame:CGRectMake(_titleLabel.right-0.7+7, _imgView.top, 0.7, kLabelHeight+_heartCountBtn.height-1)];
    rightLine.image = [UIImage imageNamed:@"gray_vertical"];
    //[self.contentView addSubview:rightLine];
    
    
    // 作品时长
    _timeLabel.frame = CGRectMake(0, 0,_imgView.bounds.size.width, _imgView.bounds.size.height);
    _timeLabel.textAlignment=NSTextAlignmentCenter;
    
    if (![self.listdataModel.duration isEqual:[NSNull null]]) {
        int fen=(int)_listdataModel.duration.integerValue/60;
        int miao=_listdataModel.duration.integerValue%60;
        NSString *wok = [NSString stringWithFormat:@"%.2d %.2d",fen,miao];
        _timeLabel.text = wok;
    }
    _timeMin.frame = CGRectMake(_timeLabel.frame.size.width / 2 - 4, _timeLabel.frame.size.height / 2 - 14, 8, 8);
    _timeSec.frame = CGRectMake(_timeLabel.frame.size.width / 10 * 8.5 , _timeLabel.frame.size.height / 2 - 14, 8, 8);
    //    _timeMin.backgroundColor =[UIColor blueColor];
    
    
    // 场景标签数量
    _scenceCountBtn.frame = CGRectMake(_imgView.right, _imgView.bottom-36, 0, 0);
    
    // 播放次数
    _playtimesBtn.frame = CGRectMake(_imgView.right, _imgView.bottom-36, 0, 0);
    [_playtimesBtn setImage:[UIImage imageNamed:@"play_gray_btn"] forState:UIControlStateNormal];
    _playtimesBtn.titleLabel.textColor = [UIColor blackColor];
    _playtimesBtn.title=self.listdataModel.play_count;
    
    //点按数量
    _heartCountBtn.frame = CGRectMake(_playtimesBtn.right - 10, _imgView.bottom-36, 0, 0);
    [_heartCountBtn setImage:[UIImage imageNamed:@"love_gray_btn"] forState:UIControlStateNormal];
    _heartCountBtn.titleLabel.textColor = [UIColor blackColor];
    _heartCountBtn.title = self.listdataModel.love_count;
//    _heartCountBtn.selected = *(self.listdataModel.liked);
    
    _edging.frame = CGRectMake(_imgView.frame.origin.x + _imgView.frame.size.width, kupGap, self.frame.size.width - kleftGap - 94, kImageViewHeight);
    if ([UIScreen mainScreen].bounds.size.height == 667) {
        _edging.frame = CGRectMake(_imgView.frame.origin.x + _imgView.frame.size.width, kupGap, self.frame.size.width - kleftGap - 115, kImageViewHeight);
    } else if ([UIScreen mainScreen].bounds.size.height == 736) {
        _edging.frame = CGRectMake(_imgView.frame.origin.x + _imgView.frame.size.width, kupGap, self.frame.size.width - kleftGap - 125, kImageViewHeight);
    }
    _edging.layer.borderWidth = 1;
    _edging.layer.borderColor = [[UIColor colorWithWhite:0.868 alpha:1.0] CGColor];
    // 处理日期字符串
    NSRange range1 = NSMakeRange(5, 2);
    NSString *month=[self.listdataModel.created_at substringWithRange:range1];
    
    NSRange range2 = NSMakeRange(8, 2);
    NSString *day=[self.listdataModel.created_at substringWithRange:range2];
    NSString *date = [NSString stringWithFormat:@"%@-%@", month, day];
    _monthLabel.text = date;
    
    _classify.frame = CGRectMake(self.frame.size.width - 75, _edging.frame.origin.y + _edging.frame.size.height - 20, 60, 12);
//    if (_listdataModel.is_private == YES) {
//        _classify.text = @"私有";
//    } else {
//        _classify.text = @"公开";
//    }
    if ([_listdataModel.tags containsObject:@"记#录"]) {
        _classify.text = @"记录";
    } else if ([_listdataModel.tags containsObject:@"点#评"]){
        _classify.text = @"点评";
    } else if ([_listdataModel.tags containsObject:@"谈#话"]){
        _classify.text = @"谈话";
    } else if ([_listdataModel.tags containsObject:@"音#乐"]){
        _classify.text = @"音乐";
    } else if ([_listdataModel.tags containsObject:@"买#卖"]){
        _classify.text = @"买卖";
    } else if ([_listdataModel.tags containsObject:@"读#书"]){
        _classify.text = @"读书";
    } else if ([_listdataModel.tags containsObject:@"话#题"]){
        _classify.text = @"话题";
    } else if ([_listdataModel.tags containsObject:@"观#点"]){
        _classify.text = @"观点";
    } else if ([_listdataModel.tags containsObject:@"段#子"]){
        _classify.text = @"段子";
    } else if ([_listdataModel.tags containsObject:@"其#他"]){
        _classify.text = @"其他";
    } else if ([_listdataModel.tags containsObject:@"故#事"]){
        _classify.text = @"故事";
    } else if ([_listdataModel.tags containsObject:@"教#学"]){
        _classify.text = @"教学";
    } else if ([_listdataModel.tags containsObject:@"旅#行"]){
        _classify.text = @"旅行";
    } else if ([_listdataModel.tags containsObject:@"电#影"]){
        _classify.text = @"电影";
    } else if ([_listdataModel.tags containsObject:@"影#评"]){
        _classify.text = @"电影";
    }
    else {
        _classify.text = @"暂无分类";
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
